/**
* \file serializer.cpp
* \brief Implementation of the Serializer class
* \date 23/09/2009
* \author Jérôme Stadelmann, Reds, Heig-vd
*/

#include "serializer.h"
#include <iostream>

//*************************************************************************************************
// Constructor
/// @param fileName The name of the file to open
//*************************************************************************************************
Serializer::Serializer(const QString& fileName)
{
    open(fileName);

    m_stream.setRealNumberNotation(QTextStream::FixedNotation);
    m_stream.setRealNumberPrecision(3);

}

//*************************************************************************************************
// Destructor
//*************************************************************************************************
Serializer::~Serializer()
{
    close();
}

//*************************************************************************************************
// Function writeDataSet
/// @param data A list of value to serialize
/// @param points The number of values in the list
//*************************************************************************************************
void Serializer::writeDataSet(short* data, int points)
{
	m_hMutex.lock();
    m_stream << "[";
    for (int i=0; i<points; i++)
    {
        m_stream << data[i] << ",";
    }
    m_stream << "]" << endl;

    m_hMutex.unlock();
}

//*************************************************************************************************
// Function writeDataSet
/// @param data A list of value to serialize
/// @param points The number of values in the list
//*************************************************************************************************
void Serializer::writeDataSet(float* data, int points)
{
	m_hMutex.lock();

    m_stream << "[";
    for (int i=0; i<points; i++)
    {
        m_stream << data[i] << ",";
    }
    m_stream << "]" << endl;

    m_hMutex.unlock();
}

//*************************************************************************************************
// Function writeSeparator
//*************************************************************************************************
void Serializer::writeSeparator()
{
    m_stream << "***************************************************************" << endl;
}

//*************************************************************************************************
// Function clear
//*************************************************************************************************
void Serializer::clear()
{
    m_stream.flush();
    m_file.resize(0);
}

//*************************************************************************************************
// Function close
//*************************************************************************************************
void Serializer::close()
{
    if (m_file.isOpen())
    {
        m_stream.flush();
        m_file.close();

        if (m_file.size() == 0)
            m_file.remove();
    }
}

//*************************************************************************************************
// Function open
/// @param fileName The name of the file to open
//*************************************************************************************************
void Serializer::open(const QString& fileName)
{
    close();

    m_file.setFileName(fileName);
    m_file.open(QIODevice::ReadWrite | QIODevice::Text);
    m_stream.setDevice(&m_file);
}

//*************************************************************************************************
// Function getNextFileName
/// @param fileName The next unused filename
//*************************************************************************************************
QString Serializer::getNextFileName(const QString& fileName)
{
    int iFileCount = 0;
    QString locFile;

    do
    {
        QString str;
        locFile = fileName;
        str.sprintf("_%d", iFileCount++);
        locFile.insert(std::max(fileName.lastIndexOf('.'), locFile.size()), str);
    }
    while (QFile::exists(locFile));

    return locFile;
}

//*************************************************************************************************
// Function saveTo
/// @param fileName The name of the file to save to
//*************************************************************************************************
QString Serializer::saveTo(const QString& fileName)
{
    QString strNewName, strOldName;

    close();
    strNewName = getNextFileName(fileName);
    strOldName = m_file.fileName();
    m_file.copy(strNewName);
    m_file.close();
    m_file.setFileName(strOldName);
    m_file.open(QIODevice::ReadWrite | QIODevice::Text);

    return strNewName;
}

//*************************************************************************************************
// Function getFileName
//*************************************************************************************************
QString Serializer::getFileName()
{
    return m_file.fileName();
}

//*************************************************************************************************
// Function logSampleNBR to record current sample indexes
//*************************************************************************************************
void Serializer::logSampleNBR(int nbCurrentCommand, int nbCurrentSample)
{
	m_hMutex.lock();
    m_stream << "COMMAND "<< nbCurrentCommand << " SAMPLE "<<nbCurrentSample<< endl;
    m_hMutex.unlock();
}

//*************************************************************************************************
// Function logSampleData to record one direction of the samples
//*************************************************************************************************
void Serializer::logSampleData(double* data, int points)
{
	m_hMutex.lock();



    for (int i=0; i<points; i++)
    {

        m_stream << data[i] << ";";
    }
    m_stream <<  endl;

    m_hMutex.unlock();
}

